package com.wowoai.chatai.controller;

import com.wowoai.chatai.controller.req.AIDrawingCallBackReq;
import com.wowoai.chatai.controller.req.GeneratePictureReq;
import com.wowoai.chatai.controller.req.UserDrawingRecordPageReqVO;
import com.wowoai.chatai.controller.vo.UserDrawingVO;
import com.wowoai.chatai.controller.vo.UserImageVO;
import com.wowoai.chatai.convert.UserDrawingConvert;
import com.wowoai.chatai.enums.MJLineEnum;
import com.wowoai.chatai.service.AIServiceI;
import com.wowoai.chatai.service.dto.UserDrawingDTO;
import com.wowoai.chatai.service.dto.UserImageDTO;
import com.wowoai.common.annotation.NotNeedAuth;
import com.wowoai.common.page.Pagination;
import com.wowoai.common.response.ResponseVO;
import com.wowoai.common.userInfo.CurrentUserInfo;
import com.wowoai.user.enums.UserScoreOptionTypeEnum;
import com.wowoai.user.service.UserServiceI;
import com.wowoai.user.service.dto.UserDTO;
import com.wowoai.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * AI
 */
@Slf4j
@RestController
@RequestMapping("/AIDrawing")
public class AIDrawingController {

    private final AIServiceI aiServiceI;

    private final UserServiceI userServiceI;

    public AIDrawingController(AIServiceI aiServiceI, UserServiceI userServiceI){
        Assert.notNull(aiServiceI,"aiServiceI 不能为空");
        Assert.notNull(userServiceI,"userServiceI 不能为空");

        this.aiServiceI = aiServiceI;
        this.userServiceI = userServiceI;
    }

    private static final String problemPrefix = "将下面的句子翻译成英语,不要带任何解释,只是输出翻译之后的句子，下面是要翻译的句子: ";


    /**
     * 生成图片发送http请求
     * @return
     */
    @PostMapping("/generatePicture")
    public ResponseVO<String> generatePicture(@Valid @RequestBody GeneratePictureReq generatePictureReq){
        UserDTO userInfo = userServiceI.getUserByPhoneNumber(CurrentUserInfo.getUserInfo().getPhoneNumber());
        if (Objects.isNull(generatePictureReq.getLine())){
            generatePictureReq.setLine(MJLineEnum.SLOW_LINE.getCode()); //默认是1 慢速路线
        }
        //减去积分
        if (Objects.equals(generatePictureReq.getLine(),MJLineEnum.SLOW_LINE.getCode())){
            userServiceI.updateScore(userInfo,6, UserScoreOptionTypeEnum.DRAWING_IMAGE,"-6");
        }else if (Objects.equals(generatePictureReq.getLine(),MJLineEnum.FAST_LINE.getCode())){
            userServiceI.updateScore(userInfo,18, UserScoreOptionTypeEnum.DRAWING_IMAGE,"-18");
        }
        //先调用chat翻译
        String promptCN = generatePictureReq.getPrompt();
        String promptEN = aiServiceI.generateChat(problemPrefix + promptCN);
        if (Objects.isNull(promptEN)){
            return ResponseVO.fail("AI绘图失败！");
        }
        generatePictureReq.setPrompt(promptEN + " --ar " + generatePictureReq.getDefine());
        //调用图片API
        String taskId = aiServiceI.generatePicture(userInfo, generatePictureReq, promptCN);
        return ResponseVO.success(taskId);
    }

    /**
     * AI绘画回调函数
     */
    @PostMapping("/aIDrawingCallBack/{userId}")
    @NotNeedAuth
    public void aIDrawingCallBack(@RequestBody AIDrawingCallBackReq aiDrawingCallBackReq, @PathVariable("userId") Long userId) {
        log.info("MJ绘画回调接收参数：{}", JsonUtils.toJsonString(aiDrawingCallBackReq));
        aiServiceI.aIDrawingHandle(userId, aiDrawingCallBackReq);
    }

    /**
     * 分页查询用户得图片
     *
     * @param reqVO
     * @return
     */
    @PostMapping("/getDrawingImagePage")
    public ResponseVO<Pagination<UserDrawingVO>> getDrawingImagePage(@RequestBody UserDrawingRecordPageReqVO reqVO) {
        //分页查询
        Pagination<UserDrawingDTO> page = aiServiceI.getDrawingImagePage(CurrentUserInfo.getUserInfo(), reqVO);

        return ResponseVO.success(page.map(UserDrawingConvert.INS::dto2VO));
    }

    /**
     * 这个不需要权限
     *
     * @param taskId 生成图片的taskId
     * @return 返回生成的图片的url
     */
    @GetMapping("/getUserImageList")
    public ResponseVO<UserImageVO> getUserImageList(@RequestParam("taskId") String taskId) {
        log.info("查询用户生成的图片 phoneNumber = {},taskId =  {}", CurrentUserInfo.getUserInfo().getPhoneNumber(), taskId);
        UserImageDTO userImageDTO = aiServiceI.getUserImageList(CurrentUserInfo.getUserInfo().getPhoneNumber(), taskId);
        return ResponseVO.success(UserDrawingConvert.INS.userImageDTO2VO(userImageDTO));

    }
}
